<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>电子书城</title>
	<link rel="stylesheet" href="${pageContext.request.contextPath}/client/css/main.css" type="text/css" />
	<script type="text/javascript" src="${pageContext.request.contextPath}/admin/jquery/jquery-1.4.2.js"></script>
	<script>
		//当商品数量发生变化时触发该方法
		/*type:表示数量变化
        *     0： 减少
        *     1：增加
        totol:库存的总量
        pid:商品id
        *     */
		function changeProductNum(type,total,pid,e) {
			var  stockBox =$(e).parent().next(); /*库存量*/
			var subTotalBox = stockBox.next();/*小计*/
			var price = parseFloat(stockBox.prev().prev().html());/*当前商品价格*/
			var showTotal = $("#showTotal");/*总计框*/
			var totalPrice = parseFloat(showTotal.html());/*总计*/
			//购物车中的数量
			var count = 0;
			//库存量
			var stockTotal = parseInt(stockBox.html());
			if (type == 0){  //减少
				var  numberBox= $(e).next("input");/*购买数量*/
				count = parseInt(numberBox.val());
				if (count <= 1) {
					alert("不能再少了！")
					return;
				}
				count = count -1;
				totalPrice=totalPrice-price;
			}
			if (type == 1){ //添加
				var  numberBox= $(e).prev("input");/*购买数量*/
				count = parseInt(numberBox.val());
				if (count >= total) {
					alert("已达到商品最大购买量");
					return
				}
				count++;
				totalPrice=totalPrice+price;
			}
			//修改input中显示的数量，并修改数据库
			//更新数据库,库存与购买量
			$.ajax({
				url:"/Cart/updateNumber.do",
				type:"post",
				contentType:"application/json",
				data:JSON.stringify({pid:pid,number:count}),
				dataType:"json",
				success:function (resp) {
					/*alert(data.message);*/
					if (resp.data == 0){
						numberBox.val(count);
						stockBox.html(total-count);
						subTotalBox.html(price*count);
						showTotal.html(totalPrice);
					}
				}
			})
		}
		//确认是否删除购物车中的商品
		function cart_del(id) {
			var msg = "您确定要删除该商品吗？";
			if (confirm(msg)==true){
				$.ajax({
					url:"/Cart/delete",
					type: "post",
					data:"id="+id,
					dataType: "json",
					success:function (resp) {
						alert(resp.message);
						if (resp.data==0){
							window.location.href = "/Cart/mycart.do";
						}
					}
				})

				return true;
			}else{
				return false;
			}
		}



	</script>
</head>
<body class="main">
<p:user/>
<jsp:include page="head.jsp" />
<jsp:include page="menu_search.jsp" />
<div id="divpagecontent">
	<table width="100%" border="0" cellspacing="0">
		<tr>
			<td>
				<div style="text-align:right; margin:5px 10px 5px 0px">
					<a href="${pageContext.request.contextPath }/index.jsp">首页</a>
					&nbsp;&nbsp;&nbsp;&nbsp;&gt;&nbsp;&nbsp;&nbsp;&nbsp;购物车
				</div>
				<table cellspacing="0" class="infocontent">
					<tr>
						<td>
							<img src="${pageContext.request.contextPath}/client/ad/page_ad.jpg" width="900" height="89" />
							<table width="100%" border="0" cellspacing="0">
								<tr>
									<td>
										<img src="${pageContext.request.contextPath}/client/images/buy1.gif" width="635" height="38" />
									</td>
								</tr>
								<tr>
									<td>
										<table cellspacing="1" class="carttable">
											<tr>
												<td width="10%">序号</td>
												<td width="30%">商品名称</td>
												<td width="10%">价格</td>
												<td width="20%">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;数量</td>
												<td width="10%">库存</td>
												<td width="10%">小计</td>
												<td width="10%">取消</td>
											</tr>
										</table>
										<!-- 循环输出商品信息 -->
										<c:set var="total" value="0" />
										<c:forEach items="${cart}" var="entry" varStatus="vs">
											<table width="100%" border="0" cellspacing="0">
												<tr>
													<td width="10%">${vs.count}</td>
													<td width="30%">${entry.product.name }</td>
													<td width="10%">${entry.product.price }</td>
													<td width="20%">
														<!-- 减少商品数量 -->
														<input type="button" value='-' style="width:20px"
															   onclick="changeProductNum(0,${entry.product.pnum},'${entry.product.id}',this)">
														<!-- 商品数量显示 -->
														<input name="text" type="text" value="${entry.number}" style="width:40px;text-align:center" />
														<!-- 增加商品数量 -->
														<input type="button" value='+' style="width:20px"
															   onclick="changeProductNum(1,${entry.product.pnum},'${entry.product.id}',this)">
													</td>
													<td width="10%">${entry.product.pnum}</td>
													<td width="10%">${entry.subTotal}</td>
													<td width="10%">
														<!-- 删除商品 -->
														<a
																style="color:#FF0000; font-weight:bold" onclick="javascript:return cart_del(${entry.id})">X</a>
													</td>
												</tr>
											</table>
											<c:set value="${total+entry.subTotal}" var="total" />
										</c:forEach>

										<table cellspacing="1" class="carttable">
											<tr>
												<td style="text-align:right; padding-right:40px;">
													<font style="color:#FF6600; font-weight:bold">合计：&nbsp;&nbsp;<span id="showTotal">${total}元</font>
												</td>
											</tr>
										</table>
										<div style="text-align:right; margin-top:10px">
											<!--继续购物 -->
											<a href="${pageContext.request.contextPath}/pageProducts.do?currentPage=1">
												<img src="${pageContext.request.contextPath}/client/images/gwc_jx.gif" border="0" />
											</a>
											&nbsp;&nbsp;&nbsp;&nbsp;
											<!--结账 -->
											<a href="${pageContext.request.contextPath}/Cart/myorder.do">
												<img src="${pageContext.request.contextPath}/client/images/gwc_buy.gif" border="0" />
											</a>
										</div>
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</div>
<jsp:include page="foot.jsp" />
</body>
</html>
